﻿#ifndef THREADWORKER_H
#define THREADWORKER_H

#include "serverthread.h"
#include <QHash>
#include <QSettings>
#include <QCoreApplication>
#include <QJsonObject>
#include <QTimer>
#include <QMutex>
#include <QWaitCondition>
#include <QFileSystemWatcher>

class DataWorker;
class Controller : public QObject
{
    Q_OBJECT
//    Q_PROPERTY(bool isUpdate READ isUpdate WRITE isUpdate NOTIFY isUpdateChanged)

public:
    Controller();

    ~Controller() {
        workerThread.quit();
        workerThread.wait();
    }
    void setUpdateControlStart(QJsonObject value);

signals:
    void sigSocketStateChange(int,QString);
    void sigMessageRev(int,int,QString);

    void sendData(QByteArray);
    void CloseTcp();

    void startInit();
public slots:
    void SocketStateChange(int,QString);
    void MessageRev(int,int,QString);

private:
    QThread workerThread;
    DataWorker *worker;
};

class DataWorker : public QThread
{
    Q_OBJECT
public:
    explicit DataWorker(QObject *parent = 0,Controller *control = nullptr,QJsonObject data = QJsonObject());
    ~DataWorker();

    void run();

    QStringList readSettings(QSettings *pSet,QString fileskey,QString filekey);

public slots:
    void startInit();
    void requestData(int type);

    void Write(QByteArray);
    void CloseTcp();
signals:
    //void getData(int,int,QString,QString);
    //id answer msg method
    void getData(QJsonObject);
    void canUpdate(bool);

    void exitApp();
private:
    QTimer *timer;

    QMutex mutex;
    QWaitCondition cond;
    bool quit;

    QJsonObject m_server;
    Controller *pControl;
//    QMap<QString,int> m_ServerData;
//    int m_type;

//    QFileSystemWatcher mWatcher;
//    QStringList m_sendValue;
//    QStringList m_tempValue;
//    QStringList m_sendData;
    serverthread *m_pserver;
};

#endif // THREADWORKER_H
